package com.wskj.web.controller.system;

import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.system.bo.SysAgreementAddBo;
import com.wskj.system.bo.SysAgreementEditBo;
import com.wskj.system.bo.SysAgreementQueryBo;
import com.wskj.system.service.ISysAgreementService;
import com.wskj.system.vo.web.SysAgreementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 协议管理Controller
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Api(value = "协议管理控制器", tags = {"协议管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/agreement")
public class SysAgreementController extends BaseController {

    private final ISysAgreementService iSysAgreementService;

    /**
     * 查询协议管理列表
     */
    @ApiOperation("协议管理列表")
    @PreAuthorize("@ss.hasPermi('system:agreement:list')")
    @GetMapping("/list")
    public TableDataInfo<SysAgreementVo> list(@Validated SysAgreementQueryBo bo) {
        return iSysAgreementService.queryPageList(bo);
    }

    /**
     * 导出协议管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:agreement:export')")
    @Log(title = "协议管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysAgreementQueryBo bo) {
        List<SysAgreementVo> list = iSysAgreementService.queryList(bo);
        ExcelUtil<SysAgreementVo> util = new ExcelUtil<>(SysAgreementVo.class);
        return util.exportExcel(list, "协议管理");
    }

    /**
     * 获取协议管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agreement:query')")
    @GetMapping("/{agreementId}")
    public AjaxResult<SysAgreementVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("agreementId") Long agreementId) {
        return AjaxResult.success(iSysAgreementService.queryById(agreementId));
    }

    /**
     * 新增协议管理
     */
    @PreAuthorize("@ss.hasPermi('system:agreement:add')")
    @Log(title = "协议管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysAgreementAddBo bo) {
        return toAjax(iSysAgreementService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改协议管理
     */
    @PreAuthorize("@ss.hasPermi('system:agreement:edit')")
    @Log(title = "协议管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysAgreementEditBo bo) {
        return toAjax(iSysAgreementService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除协议管理
     */
    @PreAuthorize("@ss.hasPermi('system:agreement:remove')")
    @Log(title = "协议管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{agreementIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] agreementIds) {
        return toAjax(iSysAgreementService.deleteWithValidByIds(Arrays.asList(agreementIds), true) ? 1 : 0);
    }

    /**
     * 导入协议管理数据
     */
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:agreement:import')")
    @Log(title = "协议管理" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysAgreementVo> util = new ExcelUtil<>(SysAgreementVo.class);
        List<SysAgreementVo> sysAgreementList;
        try {
            sysAgreementList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysAgreementService.importSysAgreement(sysAgreementList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysAgreementVo> util = new ExcelUtil<>(SysAgreementVo.class);
        return util.importTemplateExcel("协议管理数据");
    }
}
